package com.banesco.precargadepositos.interfaz.componentes;

import javax.microedition.lcdui.Font;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

public class ComboBoxBanesco extends ObjectChoiceField {

	/** Constants for the horizontal text alignment */
	public static final int TEXT_HALINGMENT_LEFT = 0;
	public static final int TEXT_HALINGMENT_CENTER = 1;
	private int _tamanofuente;
	private String fontFamily = "BBAlpha Sans";
	
	private int _colorBordeNormal = 0X999999;
	private int _colorBordeSelectedClaro = 0XFF9900;
	private int _colorBordeSelectedOscuro = 0XFF6600;
	private int _colorLetra = 0X00466F;
	private int _colorBordeError = Color.RED;

	/** Array of choices */
	String[] m_Choices;

	/** Left margin of the field */
	private int m_MarginLeft = 0;

	/** Right margin of the field */
	private int m_MarginRight = 0;

	/** Top margin of the field */
	private int m_PaddingTop = 2;

	/** Top margin of the field */
	private int m_PaddingBottom = 2;

	/** Top margin of the field */
	private int m_MarginTop = 0;

	/** Top margin of the field */
	private int m_MarginBottom = 5;

	/** Right margin of the text in the selectbox */
	private int m_TextMarginRight = 0;

	/** Left margin of the text in the selectbox */
	private int m_TextMarginLeft = 5;

	/** Horizonta text alingment */
	private int m_HTextAlignment = TEXT_HALINGMENT_LEFT;

	/** Width of the chpice box */
	private int m_ChoiceFieldWidth;

	/** Height of the field */
	private int m_Height;

	/** Determones is field focused */
	private boolean m_isFocused = false;

	/** Width of thr DropDown arrow */
	private int m_DropDownArrowWidth;

	/** Field default color */
	private int m_DefBgColor = 0xFFFFFF;

	/** Field text default color */
	private int m_DefTextColor = 0x1D4175; //Azul Banesco

	/** Field text default color */
	private int m_DefTextColorF = 0x1D4175; //Azul Banesco

	/** Field color when it is focused */
	private int m_FocusedBgColor = 0xf6921e;

	/** Field text color when it is focused */
	private int m_FocusedTextColor = 0;

	int _width = Display.getWidth() - 20;

	//Bitmap image = Bitmap.getBitmapResource("img/flecha_abajo.png");
	Bitmap _image;// = Bitmap.getBitmapResource("img/down.png");

	/**
	 * * Constructor. The width of choice box became as big as maximum length of
	 * choice strings + text marfins + derop-down arrow width. * * @param String
	 * - label of the field * @param String[] - field choices
	 * */
	public ComboBoxBanesco(String[] choices) {
		super("", choices);

		SeleccionarFotoComboBox(false);
		setearFont(false);
		m_Choices = choices;

		// dimensions of selectable field
		//Font currFont = getFont();
		m_DropDownArrowWidth = m_Height = _image.getHeight();
		m_ChoiceFieldWidth = SeteaWidthCombo(_width);
		setBackground(BackgroundFactory.createSolidBackground(Color.WHITE));
		//setPadding(2, 2, 2, 2);
		setPadding(1,0,0,0);

		XYEdges edges = new XYEdges(1, 1, 1, 1);

		XYEdges colors = new XYEdges(_colorBordeNormal,_colorBordeNormal,_colorBordeNormal,_colorBordeNormal); 
		Border borde = BorderFactory.createSimpleBorder(edges, colors, Border.STYLE_TRANSPARENT);
		setBorder(borde);
	}

	public ComboBoxBanesco(String[] choices, int width) {
		super("", choices);

		SeleccionarFotoComboBox(false);
		setearFont(false);
		m_Choices = choices;

		// dimensions of selectable field
		//Font currFont = getFont();
		m_DropDownArrowWidth = m_Height = _image.getHeight();
		m_ChoiceFieldWidth = width;
		_width = width;
		setBackground(BackgroundFactory.createSolidBackground(Color.WHITE));
		//setPadding(2, 2, 2, 2);
	}


	public ComboBoxBanesco(long style, String[] choices, int width) {
		super("",choices,0,style | Field.FIELD_VCENTER);

		SeleccionarFotoComboBox(false);
		setearFont(false);
		m_Choices = choices;

		// dimensions of selectable field
		//Font currFont = getFont();
		m_DropDownArrowWidth = m_Height = _image.getHeight();
		m_ChoiceFieldWidth = width;
		_width = width;
		setBackground(BackgroundFactory.createSolidBackground(Color.WHITE));

		//setPadding(2, 2, 2, 2);
	}

	/**
	 * Constructor del combobox personalizado
	 * 
	 * @param elementos: Elementos que poblaran el combo
	 * @param ancho: ancho del combo
	 * @param indiceInicial: indice inicial seleccionado
	 * @param estilo: estilo del combo
	 */
	public ComboBoxBanesco(String[] elementos, int ancho,int indiceInicial, long estilo) {
		super("", elementos,indiceInicial,estilo | Field.FIELD_VCENTER);

		SeleccionarFotoComboBox(false);
		setearFont(false);
		m_Choices = elementos;

		// dimensions of selectable field
		//Font currFont = getFont();
		m_DropDownArrowWidth = m_Height = _image.getHeight();
		m_ChoiceFieldWidth = ancho;
		_width = ancho;
		setBackground(BackgroundFactory.createSolidBackground(Color.WHITE));

		//setPadding(2, 2, 2, 2);
	}


	/**
	 * Set field choices * * @param String[] - array of field choices
	 * */
	public void setChoices(Object[] choices) {
		super.setChoices(choices);
		m_Choices = (String[]) choices;
	}


	public int SeteaWidthCombo(int width)
	{
		return width;
	}

	private void SeleccionarFotoComboBox(boolean tieneFocus)
	{
		int BajaResolucion = 200000;
		if( tieneFocus )
		{
			if(Display.getWidth() * Display.getHeight() > BajaResolucion)
				_image = Bitmap.getBitmapResource("img/downHighRes_focus.png");
			else
				_image = Bitmap.getBitmapResource("img/downLowRes_focus.png");
		}
		else
		{
			if(Display.getWidth() * Display.getHeight() > BajaResolucion)
				_image = Bitmap.getBitmapResource("img/downHighRes.png");
			else
				_image = Bitmap.getBitmapResource("img/downLowRes.png");
		}
	}


	/**
	 * Set field margins. This method takes an effect for * parameter to be >=
	 * 0. * * @param int - left margin of the field * @param int - right margin
	 * of the field * @param int - top margin of the field * @param int - bottom
	 * margin of the field
	 * */
	public void setMargins(int marginLeft, int marginRight, int marginTop,
			int marginBottom) {
		if (marginLeft >= 0)
			this.m_MarginLeft = marginLeft;
		if (marginRight >= 0)
			this.m_MarginRight = marginRight;
		if (marginTop >= 0)
			this.m_MarginTop = marginTop;
		if (marginBottom >= 0)
			this.m_MarginBottom = marginBottom;
	}

	/**
	 * Set horizontal text alignment in the text box. * * @param int - alignment
	 * of the text
	 * */
	public void setTextHAlignment(int alignment) {
		m_HTextAlignment = alignment;
	}

	/**
	 * Set choice box width. * * @param int - choice box width.
	 * */
	public void setChoiceWidth(int width) {
		m_ChoiceFieldWidth = width;
	}

	/**
	 * Set text font. * * @param int - font style (see Font class) * @param int
	 * - font height * @param boolean - if true control height updates according
	 * to the new font height.
	 * */
	public void setTextFont(int style, int height, boolean updateHeight) {
		this.setFont(getFont().derive(style, height));
		if (updateHeight)
			m_DropDownArrowWidth  = getFont().getHeight();
	}

	/**
	 * Method retrieves this field's preferred width. * * @return int - field's
	 * preferred width
	 * */
	public int getPreferredWidth() {
		//return m_MarginLeft + m_MarginRight + m_ChoiceFieldWidth
		//	+ getFont().getAdvance(getLabel());
		return _width;
	}

	/**
	 * Method retrieves this field's preferred height. * * @return int - field's
	 * preferred height
	 * */
	public int getPreferredHeight() {
		// return m_MarginTop + m_PaddingTop + m_PaddingBottom + m_MarginBottom
		// + m_Height+15;
		return m_Height + m_PaddingBottom + m_PaddingTop;
	}

	/**
	 * Determines if this field accepts the focus. * * @return boolean - always
	 * true.
	 * */
	public boolean isFocusable() {
		return true;
	}

	/**
	 * Retrieves this field's current focus region. * * @param XYRect - object
	 * to contain the focus rect for this field in local coordinates
	 * */
	public void getFocusRect(XYRect rect) {
		rect.set(m_MarginLeft + getFont().getAdvance(getLabel()), m_MarginTop,
				m_ChoiceFieldWidth, m_Height + m_PaddingBottom + m_PaddingTop);
	}

	/**
	 * Draws the focus indicator for this field * * @param Graphics - graphics
	 * context for drawing the focus * @param boolean - true if the focus should
	 * be set; otherwise, false.
	 * */
	protected void drawFocus(Graphics graphics, boolean on) {
		invalidate();
	}

	/**
	 * Lays out field contents. * * @param int - amount of available horizontal
	 * space. * @param int - amount of available vertical space.
	 * */
	protected void layout(int width, int height) {
		setExtent(Math.min(getPreferredWidth(), width),
				Math.min(getPreferredHeight(), height));
	}

	/**
	 * Invoked when a field receives the focus. * * @param int - Indicates from
	 * which direction the focus enters the field.
	 * */
	public void onFocus(int direction) {
		m_isFocused = true;
		invalidate();
	}

	/**
	 * Invoked when a field loses the focus.
	 * */
	public void onUnfocus() {
		m_isFocused = false;
		invalidate();
	}

	/**
	 * Method draws a edit field. * * @param Graphics - graphic context.
	 * */
	public void paint(Graphics g) {

		SeleccionarFotoComboBox(m_isFocused);

		String visibleText = (getSelectedIndex() == -1 || getSelectedIndex() >= m_Choices.length) ? "" : m_Choices[getSelectedIndex()];
		int textLength = getFont().getAdvance(visibleText);
		int labelLength = getFont().getAdvance(getLabel());
		int textY = m_MarginTop + m_PaddingTop + (m_Height - getFont().getHeight()) / 2;
		if (textY < 0)
			textY = m_MarginTop;

		// Text can be as big as text box field
		while (textLength > m_ChoiceFieldWidth - m_TextMarginLeft
				- m_TextMarginRight - m_DropDownArrowWidth
				&& visibleText.length() > 0) {
			visibleText = visibleText.substring(0, visibleText.length() - 1);
			textLength = getFont().getAdvance(visibleText);
		}
		int textX = (m_HTextAlignment == TEXT_HALINGMENT_CENTER) ? m_MarginLeft
				+ labelLength + m_TextMarginLeft + (m_ChoiceFieldWidth - m_TextMarginLeft - m_TextMarginRight
						- m_DropDownArrowWidth - textLength) / 2 :

							m_MarginLeft + labelLength + m_TextMarginLeft;

				if (textX < 0)
					textX = m_MarginLeft + labelLength + m_TextMarginLeft;

				g.setColor(Color.WHITE);
				g.drawText(getLabel(), m_MarginLeft, textY);
				
				g.setColor(_colorLetra);
				
				if (m_isFocused == false) 
				{
					g.drawText(visibleText, textX, textY);
					
					g.drawBitmap(_width - _image.getWidth(), m_MarginTop, m_ChoiceFieldWidth, m_Height + m_PaddingBottom + m_PaddingTop, _image, 0, 0);
					
					g.setColor(_colorBordeNormal);
					g.drawRect(m_MarginLeft + labelLength, 0, _width - m_MarginLeft - m_MarginRight, m_Height);
				} 
				else 
				{
					g.drawText(visibleText, textX, textY);
					
					g.drawBitmap(_width - _image.getWidth(), m_MarginTop, m_ChoiceFieldWidth, m_Height + m_PaddingBottom + m_PaddingTop, _image, 0, 0);
					
					g.setColor(_colorBordeSelectedOscuro);	
					g.drawRect(m_MarginLeft + labelLength, 0, _width - m_MarginLeft - m_MarginRight, m_Height);

					
				}
	}


	/**
	 * Metodo que se encarga de colocar el texto Resaltado en Negrita
	 * Y Metodo que establece el tipo de fuente a utilizar
	 * Se define un unico tipo de fuente para toda la aplicacion
	 * @param resaltado boolean para indicar si se debe realizar resaltado
	 */
	private void setearFont(boolean resaltado)
	{
		try 
		{			
			if(Display.getWidth() < 480)
				_tamanofuente = 17;
			else
				_tamanofuente = 26;
			int style;
			if(resaltado)
				style = Font.STYLE_BOLD;
			else
				style = Font.STYLE_PLAIN;

			setFont(getFont().derive(style,_tamanofuente));
			FontFamily fontFamily1;
			fontFamily1 = FontFamily.forName(fontFamily);
			this.setFont(fontFamily1.getFont(style,_tamanofuente));
		}
		catch (ClassNotFoundException e) {
			System.out.println("Error Label" + e.getMessage());
			e.printStackTrace();
		}

	}

}
